package com.ymk.base.cmd;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ymk.base.mybatis.Pagination;
import com.ymk.core.code.resp.RespCode;
import com.ymk.core.constants.SysConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

public class CmdSign implements Serializable {


    public CmdSign(CmdSign reqSign) {
        this.cmdCode = reqSign.cmdCode;
        this.userId = reqSign.userId;
        this.token = reqSign.token;
        this.reqCode = reqSign.reqCode;
        this.cmdVersion = reqSign.cmdVersion;
        this.mchId = reqSign.mchId;
    }

    public CmdSign(String cmdCode, String source, Integer respCode) {
        this.cmdCode = cmdCode;
        this.respCode = respCode;
        this.source = source;
    }

    public CmdSign() {

    }

    /**
     * 来源
     */
    private String scope;
    /**
     * 命令code
     */
    private String cmdCode;
    /**
     * 请求code，用于一个cmd处理多种业务
     */
    private String reqCode;
    /**
     * 响应code,用于一个cmd处理多种业务
     */
    private Integer respCode = RespCode.CODE_SUCCESS;
    /**
     * 命令版本
     */
    private String cmdVersion;
    /**
     * 消息体
     */
    private String msg;
    /**
     * token
     */
    private String token;

    /**
     * 用户id
     */
    private Integer userId;
    /**
     * 商户id
     */
    private Integer mchId;
    /**
     * 环境id
     */
    private String envId;
    /**
     * ip地址
     */
    private String ip;

    private String source = "{}";

    private String returnStruct;

    private Long exeStartTime;
    private Long exeEndTime;

    private Integer pageIndex;
    private Integer pageSize;
    private Integer pageTotal;

    public Pagination buildPage() {
        Pagination page = new Pagination();
        page.setPageSize(this.pageSize == null ? 10 : pageSize);
        page.setPageIndex(this.pageIndex == null ? 1 : pageIndex);
        page.setStart((page.getPageIndex() - 1) * page.getPageSize());
        return page;
    }

    public Pagination setPageInfo(Pagination page) {
        this.pageTotal = page.getPageTotal();
        this.pageSize = page.getPageSize();
        this.pageIndex = page.getPageIndex();
        return page;
    }

    public String getReturnStruct() {
        return returnStruct;
    }

    public void setReturnStruct(String returnStruct) {
        this.returnStruct = returnStruct;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(Integer pageTotal) {
        this.pageTotal = pageTotal;
    }

    public String getCmdCode() {
        return cmdCode;
    }

    public void setCmdCode(String cmdCode) {
        this.cmdCode = cmdCode;
    }

    public String getReqCode() {
        return reqCode;
    }

    public void setReqCode(String reqCode) {
        this.reqCode = reqCode;
    }

    public Integer getRespCode() {
        return respCode;
    }

    public void setRespCode(Integer respCode) {
        this.respCode = respCode;
    }

    public String getCmdVersion() {
        return cmdVersion;
    }

    public void setCmdVersion(String cmdVersion) {
        this.cmdVersion = cmdVersion;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }


    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getMchId() {
        return mchId;
    }

    public void setMchId(Integer mchId) {
        this.mchId = mchId;
    }

    public String getSource() {
        if (StringUtils.isEmpty(source)) {
            return "{}";
        }
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public Long getExeStartTime() {
        return exeStartTime;
    }

    public void setExeStartTime(Long exeStartTime) {
        this.exeStartTime = exeStartTime;
    }

    public Long getExeEndTime() {
        return exeEndTime;
    }

    public void setExeEndTime(Long exeEndTime) {
        this.exeEndTime = exeEndTime;
    }

    public void startExe() {
        this.exeStartTime = System.currentTimeMillis();
    }

    public void endExe() {
        this.exeEndTime = System.currentTimeMillis();
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public String getEnvId() {
        return envId;
    }

    public void setEnvId(String envId) {
        this.envId = envId;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public static class Builder {
        private CmdSign cmdSign = new CmdSign();

        public static Builder newBuild(CmdSign cmdSign) {
            Builder builder = new Builder();
            if (cmdSign != null) {
                builder.cmdSign.setReqCode(cmdSign.getReqCode());
                builder.cmdSign.setPageIndex(cmdSign.getPageIndex());
                builder.cmdSign.setPageSize(cmdSign.getPageSize());
                builder.cmdSign.setUserId(cmdSign.userId);
                builder.cmdSign.setToken(cmdSign.getToken());
                builder.cmdSign.setMchId(cmdSign.getMchId());
            }
            return builder;
        }

        public Builder setCmdCode(String scope, String cmdCode) {
            cmdSign.setScope(scope);
            cmdSign.setCmdCode(cmdCode);
            return this;
        }

        public Builder setCmdCode(String cmdCode) {
            cmdSign.setScope(SysConstants.CMD_SERVER_SCOPE);
            cmdSign.setCmdCode(cmdCode);
            return this;
        }

        public Builder setCmdVersion(String cmdVersion) {
            cmdSign.setCmdVersion(cmdVersion);
            return this;
        }

        public Builder setToken(String token) {
            cmdSign.setToken(token);
            return this;
        }

        public Builder setSource(Object source) {
            if (source instanceof String) {
                cmdSign.setSource((String) source);
            } else {
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
                cmdSign.setSource(gson.toJson(source));
            }
            return this;
        }


        public Builder setScope(String scope){
            cmdSign.setScope(scope);
            return this;
        }


        public Builder setReqCode(String reqCode) {
            cmdSign.setReqCode(reqCode);
            return this;
        }


        public Builder setExeEndTime(Long exeEndTime) {
            cmdSign.setExeEndTime(exeEndTime);
            return this;
        }

        public CmdSign build() {
            return cmdSign;
        }




        @Override
        public String toString() {
            return new Gson().toJson(cmdSign);
        }
    }
}
